﻿using System;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;

namespace m_pricelist.Proposal_Services 
{
    //Hugmyndin með þessum klasa er sú að hér séu föll sem proposal form nýtir sér.
    class ProposalService 
    {
        string osPath = "C:\\LocalData\\Marel\\Marel Pricelist 2H 2013\\Marel Proposal\\Appendix\\ProposalInfo.docx";

        #region GetFileNameFrompath
        //Er notað í Cmdproclick. Tekur inn path á Proposalskjölin sem streng. Skilar streng til baka.
        public string GetFilenameFromPath(string strPath)
        {
            string functionReturnValue = null;
            // Returns the rightmost characters of a string upto but not including the rightmost '\'
            // e.g. 'c:\winnt\win.ini' returns 'win.ini'

            if (Strings.Right(strPath, 1) != "\\" & Strings.Len(strPath) > 0)
            {
                functionReturnValue = GetFilenameFromPath(Strings.Left(strPath, Strings.Len(strPath) - 1)) + Strings.Right(strPath, 1);
            }
            return functionReturnValue;
        }
        #endregion

        #region Install Parameter
        public void InstallParameter(Type_Installation tInstallParam, Proposal proposal)
        {
            tInstallParam = new Type_Installation();

            if (proposal.b_pricelist == true)
            {
                tInstallParam.sCurrency = Convert.ToString(proposal.xlWorkBook.CustomDocumentProperties("Currency").Value);
            }
            if (proposal.b_project == true)
            {
                tInstallParam.sCurrency = Convert.ToString(proposal.xlWorkBook.Worksheets["Main Overview"].Range("_Currency"));
            }
            tInstallParam.sCurrency = proposal.xlWorkBook.CustomDocumentProperties("Currency").Value;
            tInstallParam.bIHardware = proposal.xlWorkBook.CustomDocumentProperties("i_IHardware").Value;
            tInstallParam.bIHovertime = proposal.xlWorkBook.CustomDocumentProperties("i_IHovertime").Value;
            tInstallParam.bISoftware = proposal.xlWorkBook.CustomDocumentProperties("i_ISoftware").Value;
            tInstallParam.bISovertime = proposal.xlWorkBook.CustomDocumentProperties("i_ISovertime").Value;
            tInstallParam.bTHardware = proposal.xlWorkBook.CustomDocumentProperties("i_THardware").Value;
            tInstallParam.bTHovertime = proposal.xlWorkBook.CustomDocumentProperties("i_THovertime").Value;
            tInstallParam.bTSoftware = proposal.xlWorkBook.CustomDocumentProperties("i_TSoftware").Value;
            tInstallParam.bTSovertime = proposal.xlWorkBook.CustomDocumentProperties("i_TSovertime").Value;
            tInstallParam.dHotelRate = proposal.xlWorkBook.CustomDocumentProperties("i_HotelRate").Value;
            tInstallParam.dOtherExpenses = proposal.xlWorkBook.CustomDocumentProperties("i_OtherExpenses").Value;
            tInstallParam.bIHestimate = proposal.xlWorkBook.CustomDocumentProperties("i_Hestimate").Value;
            tInstallParam.bISestimate = proposal.xlWorkBook.CustomDocumentProperties("i_Sestimate").Value;
            tInstallParam.bTHestimate = proposal.xlWorkBook.CustomDocumentProperties("i_HTestimate").Value;
            tInstallParam.bTSestimate = proposal.xlWorkBook.CustomDocumentProperties("i_STestimate").Value;
            if (proposal.b_pricelist == true)
            {
                tInstallParam.dHDaysEstimate = proposal.xlApp.WorksheetFunction.Sum(proposal.xlWorkBook.Worksheets["Output"].Range("R4:R5000"));
                tInstallParam.dHTDaysEstimate = proposal.xlApp.WorksheetFunction.Sum(proposal.xlWorkBook.Worksheets["Output"].Range("S4:S5000"));
                tInstallParam.dSDaysEstimate = proposal.xlApp.WorksheetFunction.Sum(proposal.xlWorkBook.Worksheets["Output"].Range("T4:T5000"));
                tInstallParam.dSTDaysEstimate = proposal.xlApp.WorksheetFunction.Sum(proposal.xlWorkBook.Worksheets["Output"].Range("U4:U5000"));
            }
            if (proposal.b_project == true)
            {
                tInstallParam.dHDaysEstimate = proposal.xlApp.WorksheetFunction.Sum(proposal.xlWorkBook.Worksheets["Installation"].Range("H60:H5000"));
                tInstallParam.dHTDaysEstimate = proposal.xlApp.WorksheetFunction.Sum(proposal.xlWorkBook.Worksheets["Installation"].Range("I60:I5000"));
                tInstallParam.dSDaysEstimate = proposal.xlApp.WorksheetFunction.Sum(proposal.xlWorkBook.Worksheets["Installation"].Range("J60:J5000"));
                tInstallParam.dSTDaysEstimate = proposal.xlApp.WorksheetFunction.Sum(proposal.xlWorkBook.Worksheets["Installation"].Range("K60:K5000"));
            }

            tInstallParam.dHInstalDays = (tInstallParam.bIHestimate ? tInstallParam.dHDaysEstimate : (proposal.xlWorkBook.CustomDocumentProperties("i_Hdays").Value / 10));
            tInstallParam.dHTrainingDays = (tInstallParam.bTHestimate ? tInstallParam.dHTDaysEstimate : (proposal.xlWorkBook.CustomDocumentProperties("i_HTdays").Value / 10));
            tInstallParam.dSInstalDays = (tInstallParam.bISestimate ? tInstallParam.dSDaysEstimate : (proposal.xlWorkBook.CustomDocumentProperties("i_Sdays").Value / 10));
            tInstallParam.dSTrainingDays = (tInstallParam.bTSestimate ? tInstallParam.dSTDaysEstimate : (proposal.xlWorkBook.CustomDocumentProperties("i_STdays").Value / 10));

            tInstallParam.dTravelKm = proposal.xlWorkBook.CustomDocumentProperties("i_TravelKm").Value;

            tInstallParam.dTravelTime = proposal.xlWorkBook.CustomDocumentProperties("i_TravelTime").Value;
        }
        #endregion

        #region GetLastUsedCell Venjulegt og yfirskrifað
        public int GetLastUsedCell(int ColNum, string sSheetName, ref Excel.Worksheet xlSheet)
        {
            int LastRowUsed = 0;
            try
            {
                Excel.Range namedRange = xlSheet.get_Range("C10", "M200");
                LastRowUsed = namedRange.Find("*", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                    Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlPrevious, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Row;
                if (LastRowUsed == 0)
                    throw new Exception(String.Format(
                                    "The argument {0} is not divisible by 2.",
                                    ColNum));
                else
                {
                    return LastRowUsed;
                }
            }
            catch (Exception ex)
            {
                CleanUp(osPath);
                MessageBox.Show(ex.ToString());
            }
            return LastRowUsed;
        }

        //Yfirskrifuð útgáfa af þessu falli, tekur inn reference breytu sem er Word objectið. Þetta er gert til að drepa Word skjalið sem opnast í 
        //bakgrunni ef það kemur upp villa.
        public int GetLastUsedCell(int ColNum, string sSheetName, ref Word.Application wdApp, Word.Document wdProposal, Proposal proposal)
        {
            int LastRowUsed = 0;
            try
            {
                proposal.xlSheet = (Excel.Worksheet)proposal.xlWorkBook.Worksheets.get_Item(sSheetName);
                Excel.Range namedRange = proposal.xlSheet.get_Range("C10", "M200");
                LastRowUsed = namedRange.Find("*", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, 
                    Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlPrevious, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Row;
                if (LastRowUsed == 0)
                    throw new Exception(String.Format(
                                    "The argument {0} is not divisible by 2.",
                                    ColNum));
                else
                {
                    return LastRowUsed;
                }
            }
            catch (Exception ex)
            {
                wdProposal.Close();

                wdProposal = null;
                CleanUp(osPath, ref wdApp);
                MessageBox.Show(ex.ToString());
            }
            return LastRowUsed;
        }
        #endregion

        #region CleanUp
        //Til að hreinsa upp minni
        public void CleanUp(string osPath)
        {
            //wdApp.Quit();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        //Til að hreynsa upp minni
        public void CleanUp(string osPath, ref Word.Application wdApp)
        {
            wdApp.Application.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wdApp);
            GC.Collect();
            GC.WaitForPendingFinalizers();

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        #endregion
    }
}
